package Text_69;

import java.util.ArrayList;
import java.util.Collections;
import java.util.function.Function;
import java.util.stream.Stream;

public class Code {

	public static void main(String[] args) {
		//中间流
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"张无忌","赵铭","张三峰","张三","张强","杨过","小龙女","杨过");
        Stream<String> stream1 = list.stream().filter(s -> s.startsWith("张"));
        Stream<String> stream2 = stream1.filter(s->s.length()==3);
        stream2.forEach(s-> System.out.print(s+" "));
        System.out.println("\n===========");
        //链式编程
        list.stream().filter(s -> s.startsWith("张")).filter(s->s.length()==3).forEach(s-> System.out.print(s+" "));
        System.out.println("\n===========");
        //获取前四个
        list.stream().limit(4).forEach(s-> System.out.print(s+" "));
        System.out.println();
        //跳过前四个
        list.stream().skip(4).forEach(s-> System.out.print(s+" "));
        //获取"张三峰","张三","张强"
        System.out.println();
        list.stream().skip(2).limit(3).forEach(s-> System.out.println(s));
        //去重
        System.out.println("===========");
        list.stream().distinct().forEach(s-> System.out.println(s));
        System.out.println("===========");
        //合并
        ArrayList<String> list1 = new ArrayList<>();
        Collections.addAll(list1,"郭靖","韦小宝","康熙","尔康");
        Stream.concat(list.stream(),list1.stream()).forEach(s-> System.out.println(s));
        System.out.println("===========");
        //map方法
        ArrayList<String> list2 = new ArrayList<>();
        Collections.addAll(list2,"郭靖-22","韦小宝-12","康熙-33","尔康-23");
        list2.stream().map(new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                //以"-"分割
                String[] arr = s.split("-");
                //获取数字字符串
                String ageString = arr[1];
                //String->Integer
                int age = Integer.parseInt(ageString);
                return age;
            }//s类型是整数
        }).forEach(s-> System.out.println(s));

        list2.stream().map(s -> Integer.parseInt(s.split("-")[1])).forEach(s-> System.out.println(s));

	}

}
